<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>题库管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header">题目管理</div>
        <div class="layui-card-body">

          <div class="test-table-reload-btn layui-form-item" style="margin-bottom: 10px;">
            题目内容：
            <div class="layui-inline">
              <input class="layui-input" name="questionContent" id="test-table-demoReload" autocomplete="off">
            </div>
            题目类型：
            <div class="layui-inline layui-form">
              <select name="questionType" >
                <option value="">--请选择--</option>
                <option value="0">判断题</option>
                <option value="1">单选题</option>
                <option value="2">多选题</option>
              </select>
            </div>
            所属题库：
            <div class="layui-inline layui-form">
              <select name="bankId" >
                <option value="">--请选择--</option>
              </select>
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
          </div>

          <table class="layui-hide" id="table-question" lay-filter="test-table-toolbar"></table>

          <script type="text/html" id="templet-questionType">
            {{#  if(d.questionType == 0){ }}
            <span class="layui-badge layui-bg-green">判断题</span>
            {{#  } else if(d.questionType == 1){ }}
            <span class="layui-badge layui-bg-blue">单选题</span>
            {{#  } else{ }}
            <span class="layui-badge layui-bg-orange">多选题</span>
            {{#  } }}
          </script>

          <script type="text/html" id="templet-bankName">
            <span class="layui-badge layui-bg-green">{{ d.bankName }}</span>
          </script>

          <script type="text/html" id="table-question-toolbar">
            <div class="layui-btn-container">
              <button class="layui-btn layui-btn-sm" lay-event="add">新增题目</button>
              <button class="layui-btn layui-btn-sm" lay-event="import">导入</button>
              <button class="layui-btn layui-btn-sm" lay-event="deleteBatch">批量删除</button>
              <button class="layui-btn layui-btn-sm" lay-event="export">导出</button>
            </div>
          </script>

          <script type="text/html" id="table-question-list">
            <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
          </script>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    editData = [];
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var admin = layui.admin
            ,table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,$form = $('form');

        //初始化选项卡
        initBankId($,$form,form);

        table.render({
            elem: '#table-question'
            ,url: '/admin/question/questions'
            ,page: true
            ,limit : 10
            ,limits: [10,30,60,90]
            ,loading: true   //开启翻页加载动画
            ,toolbar: '#table-question-toolbar'   //开启toolbar
            //,height: 'full-100' //高度最大自适应
            ,cols: [[
                {type:'checkbox'}
                ,{field:'questionId',  title: 'ID', sort: true ,hide:true}
                ,{field:'bankId',  title: '题库ID' ,hide:true}
                ,{field:'bankName',  title: '所属题库',width:160,templet: '#templet-bankName'}
                ,{field:'questionType',  title: '题目类型',templet: '#templet-questionType', width:120,unresize: true,align:"center"}
                ,{field:'questionContent',  title: '题目内容',width:360}
                ,{field:'questionAnswer',  title: '题目答案',width:100,align:"center"}
                ,{field:'questionAnalysis',  title: '答案解析' ,hide:true}
                ,{field:'optionA',  title: '选项A'}
                ,{field:'optionB',  title: '选项B'}
                ,{field:'optionC',  title: '选项C',hide:true}
                ,{field:'optionD',  title: '选项D',hide:true}
                ,{field:'optionE',  title: '选项E',hide:true}
                ,{field:'optionF',  title: '选项F',hide:true}
                ,{field:'createTime',  title: '创建时间', width:180, sort: true}
                ,{title: '操作',templet: '#table-question-list',width:180, unresize: true,fixed:"right",align:"center"}
            ]]

        });

        //头工具栏事件
        table.on('toolbar(test-table-toolbar)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            if(obj.event === 'add'){
                bankAdd();
            } else if(obj.event === 'import'){
                questionsImport();
            } else if(obj.event === 'deleteBatch'){
                var questionIds = '';
                var data = checkStatus.data;
                if(data.length == 0){
                    layer.alert("请选择要删除的数据");
                    return;
                }
                for(var i in data){
                    questionIds += "," + data[i].questionId;
                }
                questionIds = questionIds.substr(1);
                console.log(questionIds);
                layer.confirm('删除后数据不可恢复，确定要删除么？', function(index){
                    $.ajax({
                        url:"/admin/question/questions/",
                        async:false,
                        method:"delete",
                        dataType:"json",
                        data:{questionIds:questionIds}
                    });
                    table.reload('table-question');
                    layer.close(index);
                });
            }else if(obj.event === 'export'){
                var questionType = $('[name="questionType"]').val();
                var bankId = $('[name="bankId"]').val();
                location.href = "/admin/question/export?questionType="+questionType+"&bankId="+bankId;
            }
        });

        //监听行工具事件
        table.on('tool(test-table-toolbar)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('删除后数据不可恢复，确定要删除么？', function(index){
                    console.log(data.questionId);
                    $.ajax({
                        url:"/admin/question/question/"+data.questionId,
                        async:false,
                        method:"delete",
                        dataType:"json"
                    });
                    table.reload('table-question');
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                editData = data;
                bankEdit();
            }
        });

        //监听公开操作
        form.on('switch(test-table-public)', function(obj){
            layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        });


        //搜索
        var active = {
            reload: function(){
                var questionContent = $('input[name="questionContent"]');
                var questionType = $('[name="questionType"]');
                var bankId = $('[name="bankId"]');
                //执行重载
                table.reload('table-question', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        questionContent: questionContent.val(),
                        questionType: questionType.val(),
                        bankId: bankId.val(),
                    }
                });
            }
        };

        $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

    function bankAdd(){
        var index = layer.open({
            type: 2
            ,title: '添加题目'
            ,content: '/layuiadmin/add/questions-add.html'
            ,area: ['600px', '600px']
            ,maxmin: true
            ,btn: ['添加', '取消']
            ,yes: function(index, layero){
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            }
        });
        layer.full(index);
    }

    function bankEdit(){
        var index = layer.open({
            type: 2
            ,title: '修改题目'
            ,content: '/layuiadmin/edit/questions-edit.html'
            ,maxmin: true
            ,area: ['550px', '450px']
            ,btn: ['修改', '取消']
            ,yes: function(index, layero){
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            }
        });
        layer.full(index);
    }

    function questionsImport(){
        var index = layer.open({
            type: 2
            ,title: '导入题目'
            ,content: '/layuiadmin/add/questions-import.html'
            ,maxmin: true
            ,area: ['550px', '450px']
            /*,btn: ['导入', '取消']
            ,yes: function(index, layero){
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            }*/
        });
    }

    function initBankId($,$form,form){
        $.ajax({
            url:"/admin/questionBank/expertBanks",
            method:"get",
            dataType:"json",
            async:false,
            success:function(data){
                var html = '';
                for(var i in data){
                    html +='<option value='+data[i].bankId+'>'+data[i].bankName+'</option>';
                }
                $('select[name=bankId]').append(html);
                form.render();
            }
        });
    }

</script>
</body>
</html>